package soulkun.library.utils.permission

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import java.util.*
import kotlin.collections.ArrayList

object PermissionUtils {

    /**
     * Activity傻瓜式申请权限，返回值true则进入授权后的逻辑，false会自动申请权限
     * 需要与Activity中的onRequestPermissionsResult()方法配合
     * @param activity
     * @param context
     * @param permissionList
     * @param requestCode
     * @return true——权限已全部授权 false——权限部分未授权
     */
    fun activityCheckPermissionList(
        activity: Activity,
        context: Context,
        permissionList: List<String>,
        requestCode: Int
    ): Boolean {
        val needRequestPermissionList = getNeedRequestPermissionList(context, permissionList)
        if (needRequestPermissionList.isNotEmpty()) {
            activityRequestPermissionList(activity, needRequestPermissionList, requestCode)
            return false
        }
        return true
    }

    private fun activityRequestPermissionList(
        activity: Activity,
        permissionList: List<String>,
        requestCode: Int
    ) {
        ActivityCompat.requestPermissions(activity, permissionList.toTypedArray(), requestCode)
    }

    /**
     * Fragment傻瓜式申请权限，返回值true则进入授权后的逻辑，false会自动申请权限
     * 需要与Fragment中的onRequestPermissionsResult()方法配合
     */
    fun fragmentCheckPermissionList(
        fragment: Fragment,
        context: Context,
        permissionList: List<String>,
        requestCode: Int
    ): Boolean {
        val needRequestPermissionList = getNeedRequestPermissionList(context, permissionList)
        if (needRequestPermissionList.isNotEmpty()) {
            fragmentRequestPermissionList(fragment, needRequestPermissionList, requestCode)
            return false
        }
        return true
    }

    private fun fragmentRequestPermissionList(
        fragment: Fragment,
        permissionList: List<String>,
        requestCode: Int
    ) {
        fragment.requestPermissions(permissionList.toTypedArray(), requestCode)
    }

    private fun getNeedRequestPermissionList(
        context: Context,
        permissionList: List<String>
    ): List<String> {
        return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            val needList: MutableList<String> = ArrayList()
            for (permission in permissionList) {
                if (PackageManager.PERMISSION_GRANTED != context.checkSelfPermission(permission)) {
                    needList.add(permission)
                }
            }
            needList
        } else {
            ArrayList()
        }
    }

    fun goToPermissionSettingPage(context: Context) {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        val uri = Uri.fromParts("package", context.packageName, null)
        intent.data = uri
        context.startActivity(intent) //可用startActivityForResult方法，返回时可在onActivityResult中重新判断是否授权
    }

    /**
     * 检查是否授权
     */
    fun checkPermission(context: Context, permission: String?): Boolean {
        val result = context.packageManager.checkPermission(permission!!, context.packageName)
        return result == PackageManager.PERMISSION_GRANTED
    }

    /**
     * 申请activity的全部权限
     */
    fun requestAllPermission(activity: Activity, code: Int) {
        try {
            val packageInfo = activity.packageManager.getPackageInfo(
                activity.packageName,
                PackageManager.GET_PERMISSIONS
            )
            val requestedPermissions = packageInfo.requestedPermissions
            val mPermissionList: ArrayList<String> = ArrayList()
            for (requestedPermission in requestedPermissions) {
                if (ContextCompat.checkSelfPermission(
                        activity,
                        requestedPermission
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    mPermissionList.add(requestedPermission)
                }
            }
            val permissionArray = mPermissionList.toTypedArray()
            if (mPermissionList.size > 0) {
                ActivityCompat.requestPermissions(activity, permissionArray, code)
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
    }

}